﻿app.controller('RegisterCtrl',
    ['$scope', '$window', 'httpPlus', '$interval', '$stateParams', '$q','utils',
    function ($scope, $window, httpPlus, $interval, $stateParams, $q, utils) {
        utils.setTitle('注册会员');

        var Promise = $q.defer();
        $scope.logoUrl = undefined;
        $scope.name = "";
        $scope.tel = "";
        $scope.code = "";
        $scope.canClick = false;
        $scope.description = "获取验证码";
        var second = 59;
        var timer;
        $scope.getCode_click = function () {
            if ($scope.tel != undefined) {
                httpPlus
                    .RRRApi('MemberCenterPage/GetVerificationCode', {
                        _phoneno: $scope.tel.toString()
                    })
                    .then(function (data) {
                        timer = $interval(function () {
                            if (second <= 0) {
                                $interval.cancel(timer);
                                second = 59;
                                $scope.description = "获取验证码";
                                $scope.canClick = false;
                            } else {
                                $scope.description = second + "s后重发";
                                second--;
                                $scope.canClick = true;
                            }
                        }, 1000);
                    })
            } else {
                alert("手机号码格式不正确!");
            }
        };

        $scope.register_click = function () {
            if ($scope.name != undefined) {
                if ($scope.tel != undefined) {
                    if ($scope.code != undefined) {
                        httpPlus
                            .RRRApi('MemberCenterPage/RegisterMember', {
                                _oid: "",
                                _validcode: $scope.code,
                                _phoneno: $scope.tel,
                                _nickname: $scope.name,
                                _lid: "0002"
                            })
                            .then(function (data) {
                                $window.location.href = $stateParams.backurl;
                            }, function (error) {
                                alert(error);
                            })
                    } else {
                        alert("验证码格式不正确!")
                    }
                } else {
                    alert("手机号码格式不正确!")
                }
            } else {
                alert("昵称不能为空!")
            }

        };
        httpPlus
            .RRRApi('MemberCenterPage/InitRegisterMemberPage', { _oid: "" })
            .then(function (data) {
                $scope.logoUrl = data.LogoUrl;
            }, function (error) {
                alert(error);
            });
    }]);
